//
// SoapTrace.Net - A soap message monitoring utility. 
//
// Copyright (C) 2004 Matthew Ward
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// Matthew Ward (mrward@users.sourceforge.net)

using SoapTrace.Core.Communication;
using System;
using System.Text;
using NUnit.Framework;

namespace SoapTrace.Tests
{
	/// <summary>
	/// Tests that a soap message with invalid xml does not cause
	/// the <see cref="SoapRequest"/> object to throw an exception
	/// when we look at the <see cref="SoapMessage.WebMethod"/> property.
	/// </summary>
	[ TestFixture ]
	public class InvalidXmlSoapRequestTestFixture
	{
		/// <summary>
		/// The soap message under test.
		/// </summary>
		SoapRequest m_SoapRequest = null;
		
		/// <summary>
		/// Sets up the test fixture.
		/// </summary>
		[ TestFixtureSetUp ]
		public void FixtureInit( )
		{
			m_SoapRequest = new SoapRequest( );
			
			byte[ ] Bytes = UnicodeEncoding.UTF8.GetBytes( GetSoapRequest( ) );
			
			bool MessageReceived = false;
			
			for( int i = 0; i < Bytes.Length; ++i )
			{
				MessageReceived = m_SoapRequest.AddByte( Bytes[ i ] );
				
				if( MessageReceived )
				{
					break;
				}
			}
			
			Assert.IsTrue( MessageReceived, "Should have a message." );
		}
		
		/// <summary>
		/// Checks that the web service has been extracted
		/// correctly.
		/// </summary>
		[ Test ]
		public void WebService( )
		{
			Assert.AreEqual( "TextEJB", m_SoapRequest.WebService, "Incorrect web service name." );
		}
		
		/// <summary>
		/// Checks that the web operation has been extracted from 
		/// the SoapAction since there is no soap message body.
		/// </summary>
		[ Test ]
		public void WebOperation( )
		{
			Assert.AreEqual( "urn:CompressList", m_SoapRequest.WebMethod, "Incorrect web operation name." );			
		}
		
		/// <summary>
		/// Gets the soap request xml.
		/// </summary>
		/// <returns>A soap request.</returns>
		private string GetSoapRequest( )
		{
			return "POST /TextEJB.asmx HTTP/1.1\r\n" +
				"User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; MS Web Services Client Protocol 1.1.4322.573)\r\n" +
				"Content-Type: text/xml; charset=utf-8\r\n" +
				"SOAPAction: \"urn:CompressList\"\r\n" +
				"Expect: 100-continue\r\n" +
				"Connection: Keep-Alive\r\n" +
				"Host: localhost:7001\r\n" +
				"\r\n\r\n";
		}
	}
}
